Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce pex3 venv create. #2140

Merged
merged 3 commits into from
May 1, 2023
Merged

Conversation

jsirois
Copy link
Member

@jsirois jsirois commented Apr 30, 2023

This new sub-command can create either a venv or just populate a flat
sys.path directory entry (ala pip install --target) given a set of
requirements to resolve, potentially from a lock or an existing PEX
file, but otherwise from indexes and find links repos. Unlike the
sibling venv pex-tool sub-command, the target can be selected and,
in the flat sys.path directory entry case, it can be a foreign
platform.

Fixes #1752
Fixes #2110
Fixes #2111

@zmanji
Copy link
Collaborator

zmanji commented Apr 30, 2023

I know this PR is in a draft but I just wanted to say that instead of a --flat argument a --layout argument with the possible values of venv (default) and flat might be a little bit easier to understand.

@jsirois jsirois changed the title Intorduce pex3 venv create. Introduce pex3 venv create. Apr 30, 2023
@jsirois
Copy link
Member Author

jsirois commented Apr 30, 2023

I know this PR is in a draft but I just wanted to say that instead of a --flat argument a --layout argument with the possible values of venv (default) and flat might be a little bit easier to understand.

Yeah, I'm not super happy about a venv create sub-command having a mode where it does not, in fact, create a venv. This is almost a site-packages export from a venv, but not quite since it includes things like the bin/ dir at the top-level in the flat dir (just like pip install --target).

The thing is, the code used is >50% the same and the use cases I'm aware of are similar; so I've smooshed them together. The motivating use case for --flat or --layout flat being the AWS lambda runtime and its ilk.

Saying that out loud, perhaps --layout [venv|flat|flat-zipped].

This new subcommand can create either a venv or just populate a flat
`sys.path` directory entry (ala `pip install --target`) given a set of
requirements to resolve, potentially from a lock or an existing PEX
file, but otherwise from indexes and find links repos. Unlike the
sibling `venv` `pex-tool` subcommand, the target can be selected and,
in the flat `sys.path` directory entry case, it can be a foreign
platform.

Fixes pex-tool#1752
Fixes pex-tool#2110
@jsirois jsirois requested review from zmanji, benjyw and huonw May 1, 2023 00:33
@jsirois jsirois marked this pull request as ready for review May 1, 2023 00:33
@jsirois
Copy link
Member Author

jsirois commented May 1, 2023

Alright, reviewers thanks in advance - this is large. It's mainly just the new sub-command and its integration tests, but there was some code moved around and a small bit of new code in InstalledWheel to handle re-install into a flat directory ala pip install --target.

Creating a venv directly from a lock (subset) was what I needed for a-scie/lift, but it was easy enough to roll in features satisfying a few Pex issues as well as the unrealized goal @benjyw had over in Pants of having the lambda function package results be exactly what AWS / GCP / Azure call for in both deployed zip and, in the case of AWS, Lambda layers.

from typing import DefaultDict, Iterable, Iterator, List, Optional, Tuple, Union


def find_dist(
Copy link
Member Author

@jsirois jsirois May 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

@zmanji zmanji left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created a pex from this commit and it seems to work fine for me. I tested both venv and flat layouts from a lockfile with a local interpreter.

I don't quite understand the --force flag here with the venv layout. If I provide --force and the directory exists, is the venv destroyed and recreated? I my understanding is that the pex-tools command deletes the venv and recreates it entirely.

@jsirois
Copy link
Member Author

jsirois commented May 1, 2023

I don't quite understand the --force flag here with the venv layout. If I provide --force and the directory exists, is the venv destroyed and recreated?

Correct.

I my understanding is that the pex-tools command deletes the venv and recreates it entirely.

That is not correct when doing partial exports (--scope deps and --scope srcs). The venv tool used a more complex mechanism - a state file. Here I just always update when the dest dir already exists.

These are semantics I'm not convinced about, although a hard line is supporting the srcs / deps split. If you have alternate semantics that make more sense and support the split let me know.

Copy link
Collaborator

@huonw huonw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice!

I experimented with this to create lambda packages (including layers), and it worked well, using --complete-platform=... and --layout=flat-zipped along with either --scope=deps --prefix=python for a layer or --scope=srcs for the main package.

pex/cli/commands/venv.py Outdated Show resolved Hide resolved
tests/integration/cli/commands/test_venv_create.py Outdated Show resolved Hide resolved
@@ -549,6 +549,7 @@ def _populate_sources(
dst=dst,
exclude=(
"__main__.py",
"__pex__",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A venv PEX never needed the magic __pex__ import hook, which won't work anyhow since a venv PEX has no Pex .bootstrap code carrying the import hook implementation code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants